﻿using Authority.Client;
using CGT.OP.Common;
using OF.Security;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace CGT.OP.App_Start
{
    public class AuthenticationAttribute: ActionFilterAttribute
    {
        UserAuthority Authority = new UserAuthority();
        /// <summary>
        /// 菜单编码
        /// </summary>
        public string MenuCode { set; get; }

        public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            string content = "";
            bool isSuccess = Authority.CheckUserPageAuthority(UserContext.Current.UserID, MenuCode,ConfigHelper.GetValue("SystemCode"), ConfigHelper.GetValue("AuthorityUrl"), out content);
            if (!isSuccess)
            {
                filterContext.HttpContext.Response.Redirect("~/500.html");//否则跳转无权限页面
            }
            ResultData<bool> rData =Newtonsoft.Json.JsonConvert.DeserializeObject<ResultData<bool>>(content);
            if (rData.status!=0)
            {
                filterContext.HttpContext.Response.Redirect("~/500.html");//否则跳转无权限页面
            }
            if (rData.data ==false)
            {
                filterContext.HttpContext.Response.Redirect("~/403.html");//否则跳转无权限页面
            }
            base.OnActionExecuting(filterContext);
        }
    }
}